package com.fengdi.book.middle;

import java.util.*;

public class NumsAndString {

    public static class ThreeSum {
        public List<List<Integer>> threeSum(int[] nums) {
            if (nums == null || nums.length < 3) return null;

            Arrays.sort(nums);
            int begin = 0;
            List<List<Integer>> result = new ArrayList<>();

            while(begin < nums.length - 2 && nums[begin] <= 0) {
                if(begin > 0 && nums[begin] == nums[begin - 1]) {
                    begin++;
                    continue;
                }

                int left = begin + 1;
                int right = nums.length - 1;
                int target = -nums[begin];

                while(left < right) {
                    int sum = nums[left] + nums[right];
                    if(sum == target) {
                        List<Integer> list = new ArrayList<>();
                        result.add(Arrays.asList(nums[begin], nums[left], nums[right]));
                        // 过滤重复元素
                        while(left < right && nums[left] == nums[left + 1]) left++;
                        while(left < right && nums[right] == nums[right - 1]) right--;
                        left++;
                        right--;
                    }else if(sum < target) {
                        left++;
                    }else {
                        right--;
                    }
                }
                begin++;
            }

            return result;
        }


    }

    public static class SetZeroes{
        public void setZeroes(int[][] matrix) {
            if(matrix == null || matrix.length == 0 || matrix[0].length == 0) return;
            HashSet<Integer> xSet = new HashSet<>();
            HashSet<Integer> ySet = new HashSet<>();
            for(int i = 0; i < matrix.length; i++) {
                for(int j = 0; j < matrix[0].length; j++) {
                    if(matrix[i][j] == 0) {
                        xSet.add(i);
                        ySet.add(j);
                    }
                }
            }

            for(int x : xSet) {
                this.setZeroX(matrix, x);
            }

            for(int y : ySet) {
                this.setZeroY(matrix, y);
            }
        }

        public void setZeroX(int[][] matrix, int x) {
            if(x < 0) return;
            for(int i = 0; i < matrix[0].length; i++) {
                matrix[x][i] = 0;
            }
        }

        public void setZeroY(int[][] matrix, int y) {
            if (y < 0) return;
            for(int i = 0; i < matrix.length; i++) {
                matrix[i][y] = 0;
            }
        }
    }

    public static class GroupAnagrams {
        public List<List<String>> groupAnagrams(String[] strs) {
            HashMap<String, List<String>> map = new HashMap<>();
            for(String s : strs) {
                char[] sChars = s.toCharArray();
                Arrays.sort(sChars);
                String key = String.valueOf(sChars);

                if(map.containsKey(key)) {
                    map.get(key).add(s);
                }else {
                    map.put(key, new ArrayList<>());
                    map.get(key).add(s);
                }
            }

            return new ArrayList<>(map.values());
        }
    }

    public static class LengthOfLongestSubstring {
        public int lengthOfLongestSubstring(String s) {
            if(s.length() <= 1) {
                return s.isEmpty() ? 0 : 1;
            }

            HashMap<Character, Integer> map = new HashMap<>();
            int begin = 0;
            int end = 0;
            int max = 0;
            for(; end < s.length(); end++) {
                if(map.containsKey(s.charAt(end))) {
                    begin = Math.max(begin, map.get(s.charAt(end)) + 1);
                }

                map.put(s.charAt(end), end);
                max = Math.max(max, end - begin + 1);
            }
            return max;
        }
    }

    public static class LongestPalindrome {
        public String longestPalindrome(String s) {
            if(s.length() <= 1) {
                return s;
            }

            boolean[][] dp = new boolean[s.length()][s.length()];
            int begin = 0;
            int max = 1;
            for(int right = 1; right < s.length(); right++) {
                for(int left = 0; left <= right; left++) {
                    if(s.charAt(left) != s.charAt(right)) continue;

                    if(left == right) {
                        dp[left][right] = true;
                    }else if (right - left <= 2) {
                        dp[left][right] = true;
                    }else {
                        dp[left][right] = dp[left + 1][right - 1];
                    }

                    if(dp[left][right] && right - left + 1 > max) {
                        max = right - left + 1;
                        begin = left;
                    }
                }
            }
            return s.substring(begin, begin + max);
        }
    }

    public static class IncreasingTriplet {
        public boolean increasingTriplet(int[] nums) {
            if(nums.length < 3) return false;

            int min = Integer.MAX_VALUE;
            int secondMin = Integer.MAX_VALUE;

           for(int num : nums) {
               if(num <= min) {
                   min = num;
               }else if(num <= secondMin) {
                   secondMin = num;
               }else {
                   return true;
               }
           }

           return false;
        }
    }
}
